package com.hntnbs.websocket.servers;

import com.hntnbs.websocket.utils.SocketUtil;
import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint(value = "/singleWebSocket")
@Component
public class SingleWebSocketServer extends AbstractWebSocketServer {

    private Session session;

    @Override
    public final Session getSession() {
        return this.session;
    }

    @OnOpen
    public void onOpen(Session session) {
        this.session = session;
        SocketUtil.send(session, "新用户加入");
    }

    @OnClose
    public void onClose(Session session) {
        SocketUtil.send(session, "有用户离开");
    }

    @OnMessage
    public void onMessage(String msg) {
        System.out.println("从客服端接受的消息： " + msg);
    }

    @Override
    public void onMessage(Session session, String msg) throws InterruptedException {
        System.out.println("从客户端" + session.getId() + "接受的消息： " + msg);
    }
}
